package kuang.counter;

import java.util.concurrent.CountDownLatch;

/**
 * 计数器
 * countDownLatch.countDown() 数量-1
 * countDownLatch.await(); 等待计数器归零，然后向下执行（等待归零时，计算器就会被唤醒）
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        //总数是 6， 必须要执行的任务，再使用
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "go out");
                countDownLatch.countDown(); //数量-1
            }, String.valueOf(i)).start();
        }

        countDownLatch.await(); //等待计数器归零，然后向下执行

        System.out.println("Close Door");
    }
}
